#!/usr/bin/python3
# by Sun Smallwhite <niasw@pku.edu.cn>(https://github.com/niasw)

import json
import re

def saveJSON(filename,data,pretty=True):
  '''
# Save data in JSON format
# by Sun Smallwhite <niasw@pku.edu.cn>(https://github.com/niasw)

# saveJSON(filename,data,pretty=True), pretty: indent=4
# NOTICE: output paper index begin with 0 (different from input)
  '''
  # filename=re.sub('[/\\\]','.',filename); # by testing, 3 \ can work
  filename='./out/'+re.sub('.*[/\\\]','',filename);
  print('Saving File: '+filename);
  fileobj=open(filename,'w');
  if pretty:
   fileobj.write(json.dumps(data,sort_keys=True,indent=4,separators=(',',': ')));
  else:
   fileobj.write(json.dumps(data));
  fileobj.close();

